package com.wngbms.netty.seatdemo.client;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

@Slf4j
public class SimpleNIOServer {

    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress("127.0.0.1", 8090), 60);

        /*ServerSocket serverSocket = serverSocketChannel.socket();
        Socket socket;
        while ((socket = serverSocket.accept()) != null) {
            log.info("accept {}", socket.getRemoteSocketAddress());

        }*/

        /*serverSocketChannel.configureBlocking(false);
        SocketChannel socketChannel = serverSocketChannel.accept();
        log.info("accept");*/

        Selector selector = Selector.open();
        log.info("selector:{}", selector);

        serverSocketChannel.configureBlocking(false);
        // 必须设置成非阻塞模式
        SelectionKey selectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        log.info("key:{}", selectionKey);
    }

}
